
/*
  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is [console1: a visual console API and GUI].
  
  The Initial Developer of the Original Code is
  Zwetan Kjukov <zwetan@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s):
*/

package buRRRn.console1.GUI
    {
    import flash.display.Sprite;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    import system.drawing.drawing2D.Rectangle;
    import system.drawing.drawing2D.RoundedRectangle;
    
    public class Container extends Sprite
        {
        private var _changed:Boolean;
        private var _fullscreen:Boolean;
        private var _w:Number;
        private var _h:Number;
        
        private var _background:RoundedRectangle;
        private var _backgroundFull:Rectangle;
        private var _logger:Logger;
        
        public function Container( width:Number, height:Number )
            {
            super();
            _changed    = false;
            _fullscreen = false;
            _w          = width;
            _h          = height;
            
            addEventListener( Event.ADDED_TO_STAGE, addedToStageListener );
            addEventListener( Event.REMOVED_FROM_STAGE, removedFromStageListener );
            addEventListener( MouseEvent.CLICK, clickListener );
            _build();
            }
        
        private function _build():void
            {
            _background = new RoundedRectangle( _w, _h, 10 );
            _background.lineStyle.thickness = undefined;
            _background.fillStyle.color     = 0x555555;
            _background.fillStyle.alpha     = 0.8;
            addChild( _background );
            
            _backgroundFull = new Rectangle( _w, _h );
            _backgroundFull.lineStyle.thickness = undefined;
            _backgroundFull.fillStyle.color     = 0x555555;
            _backgroundFull.fillStyle.alpha     = 0.8;
            addChild( _backgroundFull );
            
            _logger = new Logger();
            addChild( _logger );
            
            if( fullscreen )
                {
                _background.visible = false;
                _backgroundFull.visible = true;
                }
            else
                {
                _background.visible = true;
                _backgroundFull.visible = false;
                }
            }
        
        private function _draw():void
            {
            trace( "Container._draw()" );
            
            if( fullscreen )
                {
                trace( "build fullscreen" );
                x = 0;
                y = 0;
                _background.visible = false;
                _backgroundFull.lineStyle.thickness = undefined;
                _backgroundFull.fillStyle.color     = 0x555555;
                _backgroundFull.fillStyle.alpha     = 0.8;
                _backgroundFull.setSize( stage.stageWidth, stage.stageHeight );
                _backgroundFull.visible = true;
                
                _logger.x = 0;
                _logger.y = 10;
                _logger.width = stage.stageWidth;
                _logger.height = stage.stageHeight - (10+20);
                }
            else
                {
                trace( "build window" );
                x = (stage.stageWidth - _w) / 2;
                y = (stage.stageHeight - _h) / 2;
                _backgroundFull.visible = false;
                _background.lineStyle.thickness = undefined;
                _background.fillStyle.color     = 0x555555;
                _background.fillStyle.alpha     = 0.8;
                _background.setSize( _w, _h );
                _background.visible = true;
                
                _logger.x = 1;
                _logger.y = 10;
                _logger.width = _w - 2;
                _logger.height = _h - (10+20);
                }
            
            changed = false;
            }
        
        protected function get changed():Boolean
            {
            return _changed;
            }
        
        protected function set changed( value:Boolean ):void
            {
            _changed = value;
            if( value )
                {
                redraw();
                }
            }
        
        protected function addedToStageListener( event:Event ):void
            {
            stage.addEventListener( Event.RENDER, renderListener );
            
            if( fullscreen )
                {
                x = 0;
                y = 0;
                _logger.x = 0;
                _logger.y = 10;
                _logger.width = stage.stageWidth;
                _logger.height = stage.stageHeight - (10+20);
                }
            else
                {
                x = (stage.stageWidth - _w) / 2;
                y = (stage.stageHeight - _h) / 2;
                _logger.x = 1;
                _logger.y = 10;
                _logger.width = _w - 2;
                _logger.height = _h - (10+20);
                }
            
            if( changed )
                {
                redraw();
                }
            }
        
        protected function removedFromStageListener( event:Event ):void
            {
            stage.removeEventListener( Event.RENDER, renderListener );
            }
        
        protected function renderListener( event:Event ):void
            {
            if( changed )
                {
                _draw();
                }
            }
        
        protected function clickListener( event:Event ):void
            {
            trace( "clickListener()" );
            fullscreen = !fullscreen;
            }
        
        protected function redraw():void
            {
            trace( "redraw()" );
            if( stage != null )
                {
                stage.invalidate();
                }
            }
        
        public function get fullscreen():Boolean
            {
            return _fullscreen;
            }

        public function set fullscreen( value:Boolean ):void
            {
            _fullscreen = value;
            changed = true;
            redraw();
            }
        
        public function setSize( w:Number, h:Number ):void
            {
            _w      = w;
            _h      = h;
            changed = true;
            }
        
        public function write( message:String ):void
            {
            _logger.write( message );
            }
        
        }
    
    }
